﻿using AutoMapper;
using B.S.Base.ErrorCode;
using B.S.Base.Infrastructrue;
using B.S.Base.Moain.Entities;
using B.S.Base.Write.Api.Application.Command.Products;
using MediatR;

namespace B.S.Base.Write.Api.Application.CommandHander.Products
{
    public class DelProductCommandHander : IRequestHandler<DelProductCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Product> Pro;
        private readonly IMapper mapper;

        public DelProductCommandHander(IBaseRepository<Product> pro, IMapper mapper)
        {
            Pro = pro;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(DelProductCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var obj = Pro.GetAll().Result.FirstOrDefault(x => x.ProductId == request.ProductId);
            obj.IsDelete = true;
            var res = Pro.Update(obj).Result;
            if (res == 1)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = res;
            }

            return Task.FromResult(result);
        }
    }
}
